﻿using System.Collections.Generic;

namespace ProblemsSet
{
    public class Problem_61 : BaseProblem
    {
        public override object GetResult()
        {
            long tmp = 0;
            long index = 0;
            var Starts = new List<Dictionary<long, List<long>>>();
            var Ends = new List<Dictionary<long, List<long>>>();

            var trE = new Dictionary<long, List<long>>();
            var trS = new Dictionary<long, List<long>>();
            Get4DigitsLists(MathLogic.GetTriangle, ref trE, ref trS);
            Starts.Add(trS);
            Ends.Add(trE);

            trE = new Dictionary<long, List<long>>();
            trS = new Dictionary<long, List<long>>();
            Get4DigitsLists(MathLogic.GetSquare, ref trE, ref trS);
            Starts.Add(trS);
            Ends.Add(trE);

            trE = new Dictionary<long, List<long>>();
            trS = new Dictionary<long, List<long>>();
            Get4DigitsLists(MathLogic.GetPenta, ref trE, ref trS);
            Starts.Add(trS);
            Ends.Add(trE);

            trE = new Dictionary<long, List<long>>();
            trS = new Dictionary<long, List<long>>();
            Get4DigitsLists(MathLogic.GetHexa, ref trE, ref trS);
            Starts.Add(trS);
            Ends.Add(trE);

            trE = new Dictionary<long, List<long>>();
            trS = new Dictionary<long, List<long>>();
            Get4DigitsLists(MathLogic.GetHepta, ref trE, ref trS);
            Starts.Add(trS);
            Ends.Add(trE);

            trE = new Dictionary<long, List<long>>();
            trS = new Dictionary<long, List<long>>();
            Get4DigitsLists(MathLogic.GetOcta, ref trE, ref trS);
            Starts.Add(trS);
            Ends.Add(trE);

            var result = new List<List<long>>();
            GetCycleRec(ref Starts, ref Ends, new List<int>(), new List<long>(), 5, ref result);
            foreach (var list in result)
            {
                if (list[0]/100 != list[5]%100) continue;
                long sm = 0;
                foreach (var l in list)
                {
                    sm += l;
                }
                return sm;

            }

            return -1;
        }

        private static void GetCycleRec(ref List<Dictionary<long, List<long>>> starts, ref List<Dictionary<long, List<long>>> ends, List<int> current, List<long> values, int index, ref List<List<long>> result)
        {
            var tmp = new List<int>(current) { index };
            if (values.Count == 0)
            {
                foreach (var pair in starts[index])
                {
                    foreach (var l in pair.Value)
                    {
                        var lst = new List<long>(values) { l };
                        for (var i = 0; i <= 5; i++)
                        {
                            if (current.Contains(i)) continue;
                            if (i == index) continue;

                            GetCycleRec(ref starts, ref ends, tmp, lst, i, ref result);
                        }

                    }
                }
            }
            else
            {
                if (!starts[index].ContainsKey(values[values.Count-1]%100)) return;
                foreach (var l in starts[index][values[values.Count - 1] % 100])
                {
                    var lst = new List<long>(values) {l};
                    if (lst.Count == 6)
                    {
                        result.Add(lst);
                    }
                    else
                    {
                        for (var i = 0; i <= 5; i++)
                        {
                            if (current.Contains(i)) continue;
                            if (i == index) continue;

                            GetCycleRec(ref starts, ref ends, tmp, lst, i, ref result);
                        }
                    }
                    
                }
            }
        }


        private delegate long ValueFunc(long value);


        private static List<long> Get4DigitsLists(ValueFunc func, ref Dictionary<long, List<long>> ends, ref Dictionary<long, List<long>> starts)
        {
            long index = 0;
            var res = new List<long>();
            while (true)
            {
                index++;
                var tmp = func(index);
                if (tmp < 1000) continue;
                if (tmp > 9999) break;
                res.Add(tmp);
                if (!starts.ContainsKey(tmp/100)) starts.Add(tmp/100, new List<long>());
                starts[tmp/100].Add(tmp);
                if (!ends.ContainsKey(tmp % 100)) ends.Add(tmp % 100, new List<long>());
                ends[tmp % 100].Add(tmp);
            }
            return res;
        }


        public override string Problem
        {
            get
            {
                return @"Triangle, square, pentagonal, hexagonal, heptagonal, and octagonal numbers are all figurate (polygonal) numbers and are generated by the following formulae:

Triangle	 	P3,n=n(n+1)/2	 	1, 3, 6, 10, 15, ...
Square	 	P4,n=n2	 	1, 4, 9, 16, 25, ...
Pentagonal	 	P5,n=n(3n1)/2	 	1, 5, 12, 22, 35, ...
Hexagonal	 	P6,n=n(2n1)	 	1, 6, 15, 28, 45, ...
Heptagonal	 	P7,n=n(5n3)/2	 	1, 7, 18, 34, 55, ...
Octagonal	 	P8,n=n(3n2)	 	1, 8, 21, 40, 65, ...
The ordered set of three 4-digit numbers: 8128, 2882, 8281, has three interesting properties.

The set is cyclic, in that the last two digits of each number is the first two digits of the next number (including the last number with the first).
Each polygonal type: triangle (P3,127=8128), square (P4,91=8281), and pentagonal (P5,44=2882), is represented by a different number in the set.
This is the only set of 4-digit numbers with this property.
Find the sum of the only ordered set of six cyclic 4-digit numbers for which each polygonal type: triangle, square, pentagonal, hexagonal, heptagonal, and octagonal, is represented by a different number in the set.";
            }
        }

        public override bool IsSolved
        {
            get
            {
                return true;
            }
        }

        public override object Answer
        {
            get
            {
                return 28684;
            }
        }

    }
}
